var contentJson = new Array;//上一页0，当前页1，下一页2的json对象
var pageNum = 1;//当前页
var pageSize = 40;
$().ready(function(){
	$.getProjectName = function(){  
	    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp  
	    var curWwwPath=window.document.location.href;  
	    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp  
	    var pathName=window.document.location.pathname;  
	    var pos=curWwwPath.indexOf(pathName);  
	    //获取主机地址，如： http://localhost:8083  
	    var localhostPaht=curWwwPath.substring(0,pos);  
	    //获取带"/"的项目名，如：/uimcardprj  
	    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);  
	    return(projectName);  
	} 
	$.queryTopicContent = function(page,pageSize,topicId,point){//point,0，1，2指明请求的是上一页，当前页和下一页
		var _data = {'page':page,'pageSize':pageSize,'tid':topicId}
		$.ajax({
			type: "POST",
			url: $.getProjectName()+"/topic/queryTopicContent",
			data:_data,
			async:false,
			dataType:"json",
			success: function(json){
				if(json.success == true){
					contentJson[point] = json;
				}else{
					alert(json.message);
				}
			},
			error : function(data, status, e){
				alert(e+"\\获取主题帖评论回复信息失败,请刷新页面重试");
			}
	    });
	}
	var page = $('input[name=page]');
	pageNum = parseInt(page.val());
	if(isNaN(pageNum)){
		pageNum = 1;
	}
	var tid = $('input[name=topicId]');
	var topicContentDiv = $('#topic_content');
	$.contentTable = $('#content_table');
	//下一页上一页按钮
	var nextPage = $('input[name=nextPage]');
	var prevPage = $('input[name=prevPage]');
	//页码数
	var pageNumSpan = $('#pageNumSpan');
	 var hasNextPageInt = parseInt($('input[name=hasNextPage]').val());//第一页初始化是否有下一页，1，0
	
	 $.submitTopicContent = $('input[name=submitTopicContent]');//提交评论按钮
	 
	$.initPageData = function(){
			$.queryTopicContent(pageNum+1,pageSize,tid.val(),2);
	}
	if(!isNaN(hasNextPageInt) && hasNextPageInt == 1){//第一页有下一页
		$.initPageData();//初始化下一页的数据
	}
	nextPage.click(function(){
		pageNum = pageNum+1;//当前页码:+1
		contentJson[0] = contentJson[1];//缓存当前页，为上一页的数据
		contentJson[1] = contentJson[2];//下一页的数据，为当前页
		//请求下一页的数据为contentJson[2]
		
		if(contentJson[1] == undefined){//如果当前页的数据为undefined，需要重新请求,然后封装结果为contentJson[1]
			//ajax
			$.queryTopicContent(pageNum,pageSize,tid.val(),1);
			//如果当前页有下一页，则修改标志位hasNextPage
			if(contentJson[1] == undefined || contentJson[1].success == false){
				alert('请求服务器数据失败,'+contentJson[1].message)
			}else{
				hasNextPage = contentJson[1].hasNextPage;
				if(hasNextPage == true){//进行下一页数据的ajax请求
					$.queryTopicContent(pageNum+1,pageSize,tid.val(),2);
				}
			}
		}
		if(contentJson[1] != undefined){
			//迭代contentJson[1]数据刷新帖子回复区域
			var html='<table>';
			var obj = contentJson[1].result;
			for(var i=0, l=obj.length; i<l; i++){  
				html += '<tr><td>id:'+obj[i].id+'</td><td><img alt="" src="'+$.getProjectName()+obj[i].fromUser.userPicture+'" width="40" height="40"></td><td>name:'+obj[i].fromUser.name+'</td><td>内容:'+obj[i].content +'</td><td>发表时间:'+obj[i].createTime+'</td><td>赞:'+obj[i].greateTimes+'</td><td></td></tr>';
			}
			html +='</table>'
			hasNextPage = contentJson[1].hasNextPage;
			var pageHtml;
			if(pageNum > 1 && hasNextPage == true){
				prevPage.attr("disabled",false);
				pageNumSpan.html(pageNum)
				nextPage.attr("disabled",false);
			}
			if(pageNum > 1 && hasNextPage == false){
				prevPage.attr("disabled",false);
				pageNumSpan.html(pageNum)
				nextPage.attr("disabled",true);
			}
			if(pageNum == 1 && hasNextPage == true){
				prevPage.attr("disabled",true);
				pageNumSpan.html(pageNum)
				nextPage.attr("disabled",false);
			}
			if(pageNum == 1 && hasNextPage == false){
				prevPage.attr("disabled",true);
				pageNumSpan.html(pageNum)
				nextPage.attr("disabled",true);
			}
			topicContentDiv.html(html);
			//进行下一页数据的ajax请求
			if(hasNextPage == true){//进行下一页数据的ajax请求
				$.queryTopicContent(pageNum+1,pageSize,tid.val(),2);
			}
		}
	});
	//上一页click
	prevPage.click(function(){
		pageNum = pageNum-1;//当前页码
		contentJson[2] = contentJson[1];//缓存当前页，为上一页的数据
		contentJson[1] = contentJson[0];//上一页的数据，为当前页
		//请求上一页的数据为contentJson[2]
		
		if(contentJson[1] == undefined){//如果当前页的数据为undefined，需要重新请求,然后封装结果为contentJson[1]
			//ajax
			$.queryTopicContent(pageNum,pageSize,tid.val(),1);
			if(contentJson[1] == undefined || contentJson[1].success == false){
				alert('请求服务器数据失败')
			}else{
				if(pageNum > 1){//进行上一页数据的ajax请求
					$.queryTopicContent(pageNum-1,pageSize,tid.val(),0);
				}
			}
		}
		if(contentJson[1] != undefined){
			//迭代contentJson[1]数据刷新帖子回复区域
			var html='<table>';
			var obj = contentJson[1].result;
			for(var i=0, l=obj.length; i<l; i++){
				html += '<tr><td>id:'+obj[i].id+'</td><td><img alt="" src="'+$.getProjectName()+obj[i].fromUser.userPicture+'" width="40" height="40"></td><td>name:'+obj[i].fromUser.name+'</td><td>内容:'+obj[i].content +'</td><td>发表时间:'+obj[i].createTime+'</td><td>赞:'+obj[i].greateTimes+'</td><td></td></tr>';
			}
			html +='</table>'
				hasNextPage = contentJson[1].hasNextPage;
			var pageHtml;
			if(pageNum > 1 && hasNextPage == true){
				prevPage.attr("disabled",false);
				pageNumSpan.html(pageNum)
				nextPage.attr("disabled",false);
			}
			if(pageNum > 1 && hasNextPage == false){
				prevPage.attr("disabled",false);
				pageNumSpan.html(pageNum)
				nextPage.attr("disabled",true);
			}
			if(pageNum == 1 && hasNextPage == true){
				prevPage.attr("disabled",true);
				pageNumSpan.html(pageNum)
				nextPage.attr("disabled",false);
			}
			if(pageNum == 1 && hasNextPage == false){
				prevPage.attr("disabled",true);
				pageNumSpan.html(pageNum)
				nextPage.attr("disabled",true);
			}
			topicContentDiv.html(html);
			if(pageNum > 1){//进行上一页数据的ajax请求
				$.queryTopicContent(pageNum-1,pageSize,tid.val(),0);
			}
		}
	});
	
//	//提交用户回复的主题帖
//	$.productPicturePathRegex = "^(\/[A-Za-z0-9\._])+"
//	 var re = new RegExp($.topicTitleRegex);
//	 var f  =  re.test(object.val().toString());
	$.ajaxTopicContent = function(content,topicFatherId,toUserId){
		alert('content:'+content)
		 var _data = {'tid':tid.val(),'topicContentString':content,'topicFatherId':topicFatherId,'toUserId':toUserId};
		 $.ajax({
				type: "POST",
				url: $.getProjectName()+"/topic/submitTopicContent",
				data:_data,
				async:false,
				dataType:"json",
				success: function(json){
					if(json.success == true){
						var table =$('#content_table');
						var obj = json.topicContent;
						var html = '<tr><td>id:'+obj.id+'</td><td><img alt="" src="'+$.getProjectName()+obj.fromUser.userPicture+'" width="40" height="40"></td><td>name:'+obj.fromUser.name+'</td><td>内容:'+obj.content +'</td><td>发表时间:'+obj.createTime+'</td><td>赞:'+obj.greateTimes+'</td><td></td></tr>';
						table.prepend(html);
						alert("评论成功!");
						UE.getEditor('discussContent').setContent('回复请点这里哟...');
						// UE.getEditor('discussContent').initialContent: '回复请点这里哟...';
					}else{
						alert(json.message);
					}
				},
				error : function(data, status, e){
					alert(e+"\\提交主题帖评论失败,请重试");
				}
		    });
	}
	$.submitTopicContent.click(function(){
		//$.discussContent = $("#discussContent");
		var ed = UE.getEditor('discussContent');
		var content = ed.getContent();
		//var contentText = ed.getContentTxt();//获取纯文本
		var topicUserId = $("input[name=topicUserId]").val();//当前主题帖用户的id
		//alert(content);
		//var content = 'this is a test,just a test.Don\'t worry';
		 var topicFatherId = 0;
		 var length = ed.getContentLength(true);//纯文本长度
		 if(length > 0 &&length <=300 ){
			 $.ajaxTopicContent(content,topicFatherId,topicUserId);
		 }else{
			 alert('请输入正确的评论,不能超出300字')
		 }
	});
	showSubmitForm = function(name,contentId,toUserId){
		var value = $("input[name=submitContentForm]");
		var submitHtml = '<tr id="submit_form_id"><td>回复给'+name+':<input name="input_content" /><a href="javascript:;" onclick="ajaxSubmitContent('+contentId+','+toUserId+',1);" >确认</a><a href="javascript:;" onclick="hideSubmitForm();" >取消</a></td></tr>';
		//alert('value:'+value.val());
		var submitForm = $("#submit_form_id");
		var location = $('#huifu_'+contentId);
		if(value.val() == 1){
			var flag = confirm("你有正在编辑的评论,确认放弃?");
			if(flag == true){
				submitForm.remove();
				location.append(submitHtml);
				value.val(1);
				return true;
			}else{
				//定位到正在编辑的评论
				return false;
			}
		}else{
			location.append(submitHtml);
			value.val(1);
			return true;
		}
	}
	ajaxSubmitContent = function(contentId,toUserId,type){
		//type为回复类型，1为回复的是评论，2为回复的  回复
		var value = $("input[name=submitContentForm]");
		var contentValue = $("input[name=input_content]");
		if(contentValue != undefined && contentValue.length >0){
			var _data = {'tid':tid.val(),'topicContentString':contentValue.val(),'topicFatherId':contentId,'toUserId':toUserId};
			$.ajax({
				type: "POST",
				url: $.getProjectName()+"/topic/submitTopicContent",
				data:_data,
				async:false,
				dataType:"json",
				success: function(json){
					if(json.success == true){
						var contentLocation = $("#huifu_"+contentId);
						var obj = json.topicContent;
						var html = '<tr><td>id:'+obj.id+'</td><td><img alt="" src="'+$.getProjectName()+obj.fromUser.userPicture+'" width="40" height="40"></td><td>uid:'+obj.fromUser.id+'//name:'+obj.fromUser.name+'<span style="color:red">回复</span>id:'+obj.toUser.id+'//'+obj.toUser.name+'</td><td>name:'+obj.fromUser.name+'</td><td>内容:'+obj.content +'</td><td>发表时间:'+obj.createTime+'</td><td>赞:'+obj.greateTimes+'</td><td></td></tr>';
						contentLocation.append(html);
						value.val('0');//初始化评论框为没有状态
						$("#submit_form_id").remove();//删除节点
					}else{
						alert(json.message);
					}
				},
				error : function(data, status, e){
					alert(e+"\\提交评论失败,请重试");
				}
			});
		}
	}
	
	hideSubmitForm = function(){
		$("#submit_form_id").remove();
		var value = $("input[name=submitContentForm]");
		value.val('0');
	}
	
	submitTopicGreat = function(times,obj){
		obj = $(obj);
		var _data = {'tid':tid.val()};
		$.ajax({
			type: "POST",
			url: $.getProjectName()+"/topic/ajaxTopicGreat",
			data:_data,
			async:false,
			dataType:"json",
			success: function(json){
				if(json.success == true){
					var code = json.code;
					if(code == 1){
//						alert("赞成功")
						obj.html('赞:'+(times+1));
					}
					if(code == 2){
						alert("已经赞");
					}
					if(code == 0){
						alert("操作失败");
					}
				}else{
					alert(json.message);
				}
			},
			error : function(data, status, e){
				alert(e+"\\提交失败,请重试");
			}
		});
		
	}
	
	submitContentGreat = function(contentId,times,obj){
		obj = $(obj);
		var _data = {'contentId':contentId};
		$.ajax({
			type: "POST",
			url: $.getProjectName()+"/topic/ajaxContentGreat",
			data:_data,
			async:false,
			dataType:"json",
			success: function(json){
				if(json.success == true){
					var code = json.code;
					if(code == 1){
//						alert("赞成功");
						obj.html('赞:'+(times+1));
					}
					if(code == 2){
						alert("已经赞");
					}
					if(code == 0){
						alert("操作失败");
					}
				}else{
					alert(json.message);
				}
			},
			error : function(data, status, e){
				alert(e+"\\提交失败,请重试");
			}
		});
		
	}
	
	//视频隐藏和播放加载播放器js
	openEmbed = function(obj){
		var obj = $(obj);
		var focusDiv = obj.parent();
		var embedObj = focusDiv.parent().find('div.emb_div');
		focusDiv.hide();
		embedObj.show();
	}
	hideEmbed = function(obj){
		var obj = $(obj);
		var embedObj = obj.parent();
		var focusDiv = embedObj.parent().find('div.focus_div');
		embedObj.hide();
		focusDiv.show();
	}
});